package com.example.demo.controller;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.demo.common.Result;
import com.example.demo.entity.PmDepartment;
import com.example.demo.mapper.PmDepartmentMapper;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@RestController
@RequestMapping("/pm_department")
public class PmDepartmentController {
    @Resource
    PmDepartmentMapper pmDepartmentMapper;

    @PostMapping
    public synchronized Result<?> save(@RequestBody PmDepartment pmDepartment) {
        return Result.success(pmDepartmentMapper.insert(pmDepartment));
    }

    @PutMapping
    public synchronized Result<?> update(@RequestBody PmDepartment pmDepartment) {
        return Result.success(pmDepartmentMapper.updateById(pmDepartment));
    }

    @DeleteMapping("/{id}")
    public synchronized Result<?> delete(@PathVariable Long id) {
        return Result.success(pmDepartmentMapper.deleteById(id));
    }

    @GetMapping("/{id}")
    public synchronized Result<?> get(@PathVariable Long id) {
        return Result.success(pmDepartmentMapper.selectById(id));
    }

    @GetMapping("/list")
    public synchronized Result<?> findPage(@RequestParam(defaultValue = "1") Integer pageNum,
                                           @RequestParam(defaultValue = "10") Integer pageSize,
                                           @RequestParam(defaultValue = "") String search) {
        LambdaQueryWrapper<PmDepartment> wrapper = Wrappers.lambdaQuery();
        if (StrUtil.isNotBlank(search)) {
            wrapper.like(PmDepartment::getDepartment, search);
        }
        Page<PmDepartment> pmDepartmentPage = pmDepartmentMapper.selectPage(new Page<>(pageNum, pageSize), wrapper);
        return Result.success(pmDepartmentPage);
    }
}
